package com.v2future.sjt.collegecheckingattendance.leave;

import android.view.View;
import android.widget.ListView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.leave.adapter.CourseLeaveRecordAdapter;
import com.v2future.sjt.collegecheckingattendance.leave.bean.OneCourseLeaveBean;
import com.v2future.sjt.collegecheckingattendance.message.MessageFragment;
import com.v2future.sjt.collegecheckingattendance.message.adapter.SwipeAdapter;
import com.v2future.sjt.collegecheckingattendance.message.bean.MessageBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.DensityUtil;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;
import com.v2future.sjt.collegecheckingattendance.widget.SwipeListView;
import com.v2future.sjt.collegecheckingattendance.widget.view.PullToRefreshView;

import java.io.Serializable;
import java.util.List;

/**
 * 课时假记录
 * @author shijianting
 * @time 2016/4/23 9:31
 */
public class CourseLeaveRecordActivity extends MainActivity implements PullToRefreshView.OnHeaderRefreshListener,PullToRefreshView.OnFooterRefreshListener{

    private ListView messagesLv;

    PullToRefreshView pullView;

    SwipeAdapter swipeAdapter;

    private List<MessageBean> dataList;

    /**
     * 消息页数
     */
    private int page = 1;

    /**
     * 总页数
     */
    private int totalPage = 1;

    @Override
    public void init() {

    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_course_leave_record);
        initTitleBack(null);
        setTitleText("申请课时假记录");
        pullView = (PullToRefreshView) findViewById(R.id.pull_view);
//        pullView.setFooterViewVisibility(View.GONE);
        pullView.setOnHeaderRefreshListener(this);
        pullView.setOnFooterRefreshListener(this);
        messagesLv = (ListView) findViewById(R.id.lv_course_leave_record);
    }

    @Override
    public void initAfter() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        showTitleLoading();
        doGetMessageList(page+"", "10");
    }

    private void refreshView(){
        swipeAdapter = new SwipeAdapter(this, dataList, DensityUtil.dip2px(this, 120));
        messagesLv.setAdapter(swipeAdapter);
//        messagesLv.setOnItemClickListener(this);
        if (dataList.size() == 0) {
            tip("没有待处理的消息");
        }
    }

    private void doGetMessageList(final String page, final String rowNum){
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.MESSAGE_LIST));
        params.addQueryStringParameter("page", page);
        params.addQueryStringParameter("rowNum", rowNum);
        params.addQueryStringParameter("userType", getUserInfo().getData().getSourceTypeId());
        NetUtils.doGetRequest(this, params, new NetTaskListener<MessageFragment.MessageResponeBean>() {

            @Override
            public void onSuccess(MessageFragment.MessageResponeBean result) {
                if (null != result.getTotalPage()) {
                    try {
                        totalPage = Integer.parseInt(result.getTotalPage());
                    }catch (Exception e){}
                }
                if (result.getData().size() == 0) {
                    return;
                }
                if (CourseLeaveRecordActivity.this.page == 1) {
                    dataList = result.getData();
                }
                else {
                    dataList.addAll(result.getData());
                }
                refreshView();
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(MessageFragment.MessageResponeBean result) {
                tip(result.getFailReason());
            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {
                cancelLoadingDialog();
                cancelTitleLoading();
                pullView.onHeaderRefreshComplete();
                pullView.onFooterRefreshComplete();
            }

            @Override
            public void onSuccessCache(MessageFragment.MessageResponeBean result) {

            }
        });
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        if (page >= totalPage) {
            tip("已到尾页");
            pullView.onFooterRefreshComplete();
            return;
        }
        page++;
        doGetMessageList(page+"", "10");
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        page = 1;
        doGetMessageList(page+"", "10");
    }
}
